-- Revision 136
local biz_name = "_fr.js"

if test then
    ConfigRedisConnectors[biz_name]={
        host = test.host,
        port = test.port,
        queue = "queue_1"
    }
else
    ConfigRedisConnectors[biz_name]={
			host = "192.168.5.201",
            port = 6380,
            queue = "queue_1"
    }
end

local function _fr_js()
  return coroutine.create(function ()
    -- constants
    local REDIS_SEPERATOR = config.SEPARATE_SYN
    local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
    local get_cmd_format = "get %s "
    local http_cookie_domain = config.http.setcookie.pc

    -- headers
    local local_config = config
    local COOKIE_HEADER = local_config.COOKIE_HEADER
    local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
    local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER
    local REFERER_HEADER = local_config.REFERER_HEADER
    local SERVER_HEADER = local_config.SERVER_HEADER
    local PRAGMA_HEADER = local_config.PRAGMA_HEADER
    local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
    local EXPIRES_HEADER = local_config.EXPIRES_HEADER
    local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
    local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
    --cookies
    local REGION_COOKIE = local_config.REGION_COOKIE
    local GUID_COOKIE = local_config.GUID_COOKIE
    local EXPIRE_TIME = local_config.EXPIRE_TIME
    -- functions
    local reply_pushHeader = reply.pushHeader
    local reply_ok = reply.status_type.ok
    local reply_setStatus = reply.setStatus
    local reply_appendContent = reply.appendContent
    local request_getHeadersKV = request.getHeadersKV
    local request_getParamsKV = request.getParamsKV
    local os_time = os.time
    local os_date = os.date
    local to_number = tonumber
    local util_redis_cmd = util.redis_cmd
    local util_parseKV = util.parseKV
    local luredis_clear_buffer = luredis.clear_buffer
    local luredis_send_cmd = luredis.send_cmd
    local coroutine_yield = coroutine.yield

    while true do
        -- reply
        reply_pushHeader(CACHE_CONTROL_HEADER, "max-age=300, must-revalidate")
        reply_pushHeader(PRAGMA_HEADER, "no-cache")
        reply_pushHeader(EXPIRES_HEADER, os_date("%a, %d %b %Y %H:%M:%S",os_time() + 300))
        reply_pushHeader(CONTENT_TYPE_HEADER, "text/javascript; charset=UTF-8")
        reply_setStatus(reply_ok)

        -- headers
        local headerTable = request_getHeadersKV();
        -- cookie
        local cookieString = headerTable[COOKIE_HEADER];
        local cookieTable = util_parseKV(cookieString,";=");
        local guid = cookieTable[GUID_COOKIE];
        -- params
        local paraTable = request_getParamsKV("&=");
        local z = paraTable["z"] -- zone

        if z then
            reply_appendContent("var __arCPM_",z," = [")
            if guid then
                local result = util_redis_cmd(biz_name,false,
                    luredis_send_cmd,get_cmd_format,guid.."_"..z);
                if type(result) == "string" then
                    reply_appendContent(result)
                end
            end
            reply_appendContent("];")
        end

        coroutine_yield()
        end
      end)
    end

creators[biz_name] = function()
    f[biz_name] = _fr_js()
end
